$(function() {

    'use strict';

    var video = {

        _elTpl : '<div style="display:none;position:fixed;z-index:999999;' +
                     'left:0;right:0;top:0;bottom:0;' +
                     'background:rgba(0,0,0,0.75);">' +
                   '<video src="" style="position:absolute;width:100%;' +
                       'left:0;right:0;top:50%;"></video>' +
                 '</div>',

        _popEl    : null,
        _videoEl  : null,
        _videoCtl : null,

        open: function(url) {
            this._popEl.show();
            this._videoEl.attr('src', url);
            this._videoCtl.play();
            return this;
        },

        close: function() {
            this._videoCtl.pause();
            this._popEl.hide();
            this._videoEl.attr('src', '');
            return this;
        },

        init: function() {

            var that = this,
                height;

            this._popEl    = $(this._elTpl);
            this._videoEl  = this._popEl.find('video');
            this._videoCtl = this._videoEl[0];

            height = $(window).width() * 9 / 16;

            this._videoEl.css({
                height    : height,
                marginTop : height / -2
            });

            this._videoEl.bind('touchstart', function(e) {
                e.stopPropagation();
            });

            this._popEl.bind('touchstart', function() {
                that.close();
                return false;
            });

            $('body').append(this._popEl);

            return this;
        }
    };

    video.init();

    $('.videoBtn').bind('touchstart', function() {
        video.open($(this).data('src'));
        return false;
    });

    var $page = $('.page');
    var $page4 = $('.page4');
    var content = document.getElementById('content');
    var page4 = document.getElementById('page4');
    var $wrapper = $('.wrapper');
    var $bookBtn = $('.bookBtn');
    var $telNum = $('#telNum');
    var telNum = '';
    var niepass = '';
    var code = '';
    var telNumReg = /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
    var page ={
        _moveBusy: false,
        startY: 0,
        endY: 0,
        direction: 'down',
        _fixPage: function(){
            $('.wrapper').height($(window).height());
        },
        changePage: function(index){
            $page.eq(index).addClass('active')
                 .siblings().removeClass('active');
        },
        movePrev: function(){
            var _this = this;
            $page.each(function(index, el) {
                if ($(this).hasClass('active')) {
                    if(index>0&&index!=$page.length-1){
                        _this.changePage(index-1);
                        return false;
                    }
                    return false;
                };
            });
        },
        moveNext: function(){
            var _this = this;
            $page.each(function(index, el) {
                if ($(this).hasClass('active')) {
                    if(index<$page.length-1){
                        console.log(index);
                        _this.changePage(index+1);
                        return false;
                    }else if(index==$page.length-1){
                        $('.arrow').hide();
                        $('.page1, .page2, .page3').remove();
                        $wrapper.css('overflow-y','auto');
                        $('.content').css('height','auto');
                        $('.book-wrap').hide();
                        $('#gw').addClass('active');
                    }

                    // return false;
                };
            });
        },
        setDirec: function(disY){
            if (disY>0) {
                this.direction = 'down';
            } else if(disY<0){
                this.direction = 'up';
            };
        },
        move: function(){
            var _this = this;

            function onStart(e) {
                event.preventDefault();
                _this.startY = e.targetTouches[0].pageY;
            }

            function onEnd(e) {
                _this.endY = e.changedTouches[0].pageY;
                _this.setDirec(_this.endY-_this.startY);
                if(Math.abs(_this.endY-_this.startY)>10){
                    if(_this.direction=='down'){
                        if ($wrapper.scrollTop()==0) {
                            _this.movePrev();
                        }else{
                            return false;
                        }
                    }else if(_this.direction=='up'){
                        _this.moveNext();
                    }
                }else{
                    return false;
                }
            }

            content.addEventListener('touchstart', onStart);
            content.addEventListener('touchend',onEnd);
        },

        pageBind: function(){
            $bookBtn.click(function(){
                $('.pop').show();
                // telNum = $telNum.val();
                // if(telNum==''){
                //     alert("请输入手机号码！");
                // }else{
                //     if(!telNum.match(telNumReg)){
                //         alert("请填写正确的手机号码！");
                //         return false;
                //     }else{
                //         $('.pop').show();
                //         $('#telNumber').val(telNum);
                //     }
                // }
            });

            $('.closePop').click(function(){
                $('.pop').hide();
                // $telNum.val('');
            });

            $('#getCode').click(function(){
                // var verifyPhoneNum = function(num){
                //     return /^(13|14|15|17|18)\d{9}$/.test(num);
                // };
                // var verifyEmailNum = function(email){
                //     return 
                // };
                // var os = $('.pop-left input[name="system"]:checked').val();
                // var num = $('#telNumber').val();
                // var email = $('#niePass').val();
                // var url = 'http://mobile-game-appoint.webapp.163.com/appoint/h22/'
                // if(verifyPhoneNum(num)){
                //     this._doAjax(url+num+'/'+os+'/?extra='+email,null,function(resp){
                //         if(resp.status == 'ok'){
                //             alert('获取验证码成功！');
                //         }else{
                //             alert(resp.status);
                //         }
                //     },function(){
                //         alert('获取验证码失败，请重新预约！');
                //     });
                // }else{
                //     alert('请填写正确的手机号码或者邮箱地址！');
                // }
            });

            $('#bookbtn').click(function(){
                niepass = $('#niePass').val();
                code = $('#yzCode').val();
                if(niepass == ''){
                    alert("请输入网易通行证！");
                }else if(! /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(myUsername)){
                    alert("输入的网易通行证格式不正确！");
                    return false;
                }else if(niepass == ''){
                    alert("请输入验证码！");
                }else{
                     $.ajax({
                         url:' '+telNum,
                         dataType: 'jsonp',
                         success : function(res){

                         },
                         error: function(){
                             alert("出现错误，请重试。");
                         }
                    });
                }


                // var verifyPhoneNum = function(num){
                //     return /^(13|14|15|17|18)\d{9}$/.test(num);
                // };
                // var verifyEmailNum = function(email){
                //     return 
                // };
                // var os = $('.pop-left input[name="system"]:checked').val();
                // var num = $('#telNumber').val();
                // var email = $('#niePass').val();
                // var code = $('#yzCode').val();
                // var url = 'http://mobile-game-appoint.webapp.163.com/appoint_submit_authcode/h22/'
                // if(verifyPhoneNum(num)){
                //     this._doAjax(url+num+'/'+os+'/?extra='+email + '&auth_code='+code,null,function(resp){
                //         if(resp.status == 'ok'){
                //             alert('预约成功');
                //         }else{
                //             alert(resp.status);
                //         }
                //     },function(){
                //         alert('预约失败，请重新预约！');
                //     });
                // }else{
                //     alert('请填写正确的手机号码或者邮箱地址！');
                // }

            })



        },

        share:function(){
            var shareInfo = {
                title:'《来自星星的你》出续作啦！居然是一款游戏！'
            }

            nie.use(["nie.util.mobiShare"],function(){
                $(function(){
                    MobileShare.setting({//设置分享文案
                        title: shareInfo.title,
                        callback:function(o){
                        }//分享后的回调
                     })
                    var topshare = MobileShare.create({//创建分享条
                        contentDiv:$("#NIE-topshare"),//放置分享条的容器
                        wxBgColor:"transparent",//微信分享提示框背景颜色（若skin为light,同时为分享按钮的颜色）
                        publicName:shareInfo.publicName,//公众号名
                        shareBtnText:"立即分享给好友",//微信分享按钮文字
                        shareText:shareInfo.desc,//微信分享提示框第二行文字可自定义
                        type:"bottom",//分享条的位置（top或bottom），默认为top,注意在微信中顶部分享条不显示
                        skin:"dark",//设置皮肤，有两种，dark/light,默认为dark
                        // webImg: shareInfo.webImg,//微信分享提示框icon图片
                        callback:function(){
                        }//分享条生成之后的回调
                    });
                });
            });
        },
        sharePop:function(){
                $(".shareBtn").click(function(){
                    alert();
                    $("#pop-share").show();
                    // $("#pop-share").bind("touchmove", function (event) {
                    //     event.preventDefault();
                    // })
                    // var st=setTimeout(function(){
                    //     $("#pop-share").addClass("show");
                    // },50)
                event.stopPropagation();
                })
                $(".gw").click(function(){
                    $("#pop-share").hide();

                    // var st=setTimeout(function(){
                    //     $("#pop-share").hide();
                    // },300);
                })
                $("#NIE-topshare").bind("click",function(e){
                    e.stopPropagation();
                })
            },
        init: function(){
            $('.page1').addClass('active');
            this._fixPage();
            this.move();
            this.pageBind();
            this.share();
            this.sharePop();
        }
    };

    page.init();


});
